Panda3D

fisheyeLens.cxx

00001 // Filename: fisheyeLens.cxx
00002 // Created by:  drose (12Dec01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "fisheyeLens.h"
00016 #include "deg_2_rad.h"
00017 
00018 TypeHandle FisheyeLens::_type_handle;
00019 
00020 // This is the focal-length constant for fisheye lenses.  The focal
00021 // length of a fisheye lens relates to its fov by the equation:
00022 
00023 //   w = Fd/k
00024 
00025 // Where w is the width of the negative, F is the focal length, and d
00026 // is the total field of view in degrees.
00027 
00028 // k is chosen to make the focal lengths for a fisheye lens roughly
00029 // correspond to the equivalent field of view for a conventional,
00030 // perspective lens.  It was determined empirically by simple
00031 // examination of a couple of actual lenses for 35mm film.  I don't
00032 // know how well this extends to other lenses and other negative
00033 // sizes.
00034 
00035 static const float fisheye_k = 60.0f;
00036 // focal_length = film_size * fisheye_k / fov;
00037 
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //     Function: FisheyeLens::make_copy
00041 //       Access: Public, Virtual
00042 //  Description: Allocates a new Lens just like this one.
00043 ////////////////////////////////////////////////////////////////////
00044 PT(Lens) FisheyeLens::
00045 make_copy() const {
00046   return new FisheyeLens(*this);
00047 }
00048 
00049 ////////////////////////////////////////////////////////////////////
00050 //     Function: FisheyeLens::extrude_impl
00051 //       Access: Protected, Virtual
00052 //  Description: Given a 2-d point in the range (-1,1) in both
00053 //               dimensions, where (0,0) is the center of the
00054 //               lens and (-1,-1) is the lower-left corner,
00055 //               compute the corresponding vector in space that maps
00056 //               to this point, if such a vector can be determined.
00057 //               The vector is returned by indicating the points on
00058 //               the near plane and far plane that both map to the
00059 //               indicated 2-d point.
00060 //
00061 //               The z coordinate of the 2-d point is ignored.
00062 //
00063 //               Returns true if the vector is defined, or false
00064 //               otherwise.
00065 ////////////////////////////////////////////////////////////////////
00066 bool FisheyeLens::
00067 extrude_impl(const LPoint3f &point2d, LPoint3f &near_point, LPoint3f &far_point) const {
00068   // Undo the shifting from film offsets, etc.  This puts the point
00069   // into the range [-film_size/2, film_size/2] in x and y.
00070   LPoint3f f = point2d * get_film_mat_inv();
00071 
00072   // First, get the vector from the center of the film to the point,
00073   // and normalize it.
00074   LVector2f v2(f[0], f[1]);
00075 
00076   LPoint3f v;
00077 
00078   float r = v2.length();
00079   if (r == 0.0f) {
00080     // Special case: directly forward.
00081     v.set(0.0f, 1.0f, 0.0f);
00082 
00083   } else {
00084     v2 /= r;
00085 
00086     // Now get the point r units around the circle in the YZ plane.
00087     float focal_length = get_focal_length();
00088     float angle = r * fisheye_k / focal_length;
00089     float sinAngle, cosAngle;
00090     csincos(deg_2_rad(angle), &sinAngle, &cosAngle);
00091 
00092     LVector3f p(0.0, cosAngle, sinAngle);
00093 
00094     // And rotate this point around the Y axis.
00095     v.set(p[0]*v2[1] + p[2]*v2[0],
00096           p[1],
00097           p[2]*v2[1] - p[0]*v2[0]);
00098   }
00099 
00100   // And we'll need to account for the lens's rotations, etc. at the
00101   // end of the day.
00102   const LMatrix4f &lens_mat = get_lens_mat();
00103 
00104   near_point = (v * get_near()) * lens_mat;
00105   far_point = (v * get_far()) * lens_mat;
00106   return true;
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: FisheyeLens::extrude_vec_impl
00111 //       Access: Protected, Virtual
00112 //  Description: Given a 2-d point in the range (-1,1) in both
00113 //               dimensions, where (0,0) is the center of the
00114 //               lens and (-1,-1) is the lower-left corner,
00115 //               compute the vector that corresponds to the view
00116 //               direction.  This will be parallel to the normal on
00117 //               the surface (the far plane) corresponding to the lens
00118 //               shape at this point.
00119 //
00120 //               See the comment block on Lens::extrude_vec_impl() for
00121 //               a more in-depth comment on the meaning of this
00122 //               vector.
00123 //
00124 //               The z coordinate of the 2-d point is ignored.
00125 //
00126 //               Returns true if the vector is defined, or false
00127 //               otherwise.
00128 ////////////////////////////////////////////////////////////////////
00129 bool FisheyeLens::
00130 extrude_vec_impl(const LPoint3f &point2d, LVector3f &vec) const {
00131   LPoint3f near_point, far_point;
00132   if (!extrude_impl(point2d, near_point, far_point)) {
00133     return false;
00134   }
00135 
00136   vec = far_point - near_point;
00137 
00138   return true;
00139 }
00140 
00141 ////////////////////////////////////////////////////////////////////
00142 //     Function: FisheyeLens::project_impl
00143 //       Access: Protected, Virtual
00144 //  Description: Given a 3-d point in space, determine the 2-d point
00145 //               this maps to, in the range (-1,1) in both dimensions,
00146 //               where (0,0) is the center of the lens and
00147 //               (-1,-1) is the lower-left corner.
00148 //
00149 //               Some lens types also set the z coordinate of the 2-d
00150 //               point to a value in the range (-1, 1), where 1
00151 //               represents a point on the near plane, and -1
00152 //               represents a point on the far plane.
00153 //
00154 //               Returns true if the 3-d point is in front of the lens
00155 //               and within the viewing frustum (in which case point2d
00156 //               is filled in), or false otherwise.
00157 ////////////////////////////////////////////////////////////////////
00158 bool FisheyeLens::
00159 project_impl(const LPoint3f &point3d, LPoint3f &point2d) const {
00160   // First, account for any rotations, etc. on the lens.
00161   LVector3f v2 = point3d * get_lens_mat_inv();
00162 
00163   // A fisheye lens projection has the property that the distance from
00164   // the center point to any other point on the projection is
00165   // proportional to the actual distance on the sphere along the great
00166   // circle.  Also, the angle to the point on the projection is equal
00167   // to the angle to the point on the sphere.
00168 
00169   // First, get the straight-line distance from the lens, and use it
00170   // to normalize the vector.
00171   float dist = v2.length();
00172   v2 /= dist;
00173 
00174   // Now, project the point into the XZ plane and measure its angle
00175   // to the Z axis.  This is the same angle it will have to the
00176   // vertical axis on the film.
00177   LVector2f y(v2[0], v2[2]);
00178   y.normalize();
00179 
00180   if (y == LVector2f(0.0f, 0.0f)) {
00181     // Special case.  This point is either directly ahead or directly
00182     // behind.
00183     point2d.set(0.0f, 0.0f, 
00184                 (get_near() - dist) / (get_far() - get_near()));
00185     return v2[1] >= 0.0f;
00186   }
00187 
00188   // Now bring the vector into the YZ plane by rotating about the Y
00189   // axis.
00190   LVector2f x(v2[1], v2[0]*y[0]+v2[2]*y[1]);
00191 
00192   // Now the angle of x to the forward vector represents the distance
00193   // along the great circle to the point.
00194   float r = 90.0f - rad_2_deg(catan2(x[0], x[1]));
00195 
00196   float focal_length = get_focal_length();
00197   float factor = r * focal_length / fisheye_k;
00198 
00199   point2d.set
00200     (y[0] * factor,
00201      y[1] * factor,
00202      // Z is the distance scaled into the range (1, -1).
00203      (get_near() - dist) / (get_far() - get_near())
00204      );
00205 
00206   // Now we have to transform the point according to the film
00207   // adjustments.
00208   point2d = point2d * get_film_mat();
00209 
00210   return
00211     point2d[0] >= -1.0f && point2d[0] <= 1.0f && 
00212     point2d[1] >= -1.0f && point2d[1] <= 1.0f;
00213 }
00214 
00215 ////////////////////////////////////////////////////////////////////
00216 //     Function: FisheyeLens::fov_to_film
00217 //       Access: Protected, Virtual
00218 //  Description: Given a field of view in degrees and a focal length,
00219 //               compute the correspdonding width (or height) on the
00220 //               film.  If horiz is true, this is in the horizontal
00221 //               direction; otherwise, it is in the vertical direction
00222 //               (some lenses behave differently in each direction).
00223 ////////////////////////////////////////////////////////////////////
00224 float FisheyeLens::
00225 fov_to_film(float fov, float focal_length, bool) const {
00226   return focal_length * fov / fisheye_k;
00227 }
00228 
00229 ////////////////////////////////////////////////////////////////////
00230 //     Function: FisheyeLens::fov_to_focal_length
00231 //       Access: Protected, Virtual
00232 //  Description: Given a field of view in degrees and a width (or
00233 //               height) on the film, compute the focal length of the
00234 //               lens.  If horiz is true, this is in the horizontal
00235 //               direction; otherwise, it is in the vertical direction
00236 //               (some lenses behave differently in each direction).
00237 ////////////////////////////////////////////////////////////////////
00238 float FisheyeLens::
00239 fov_to_focal_length(float fov, float film_size, bool) const {
00240   return film_size * fisheye_k / fov;
00241 }
00242 
00243 ////////////////////////////////////////////////////////////////////
00244 //     Function: FisheyeLens::film_to_fov
00245 //       Access: Protected, Virtual
00246 //  Description: Given a width (or height) on the film and a focal
00247 //               length, compute the field of view in degrees.  If
00248 //               horiz is true, this is in the horizontal direction;
00249 //               otherwise, it is in the vertical direction (some
00250 //               lenses behave differently in each direction).
00251 ////////////////////////////////////////////////////////////////////
00252 float FisheyeLens::
00253 film_to_fov(float film_size, float focal_length, bool) const {
00254   return film_size * fisheye_k / focal_length;
00255 }
00256 
 All Classes Functions Variables Enumerations