Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

sglUnScaleRange.hpp

00001 /*****************************************************************************
00002  * SGL: A Scene Graph Library
00003  *
00004  * Copyright (C) 1997-2001  Scott McMillan   All Rights Reserved.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the Free
00018  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *****************************************************************************
00020  *     File: sglUnScaleRange.hpp
00021  *   Author: Scott McMillan
00022  *  Created: 6 May 2002
00023  *  Summary: Dynamically scales to maintain a constant minimum pixel size
00024  *           beyond a certain range, and a constant maximum pixel size inside
00025  *           a certain range (the user only specifies the min and max sizes).
00026  *****************************************************************************/
00027 
00028 #ifndef __SGL_UNSCALE_RANGE_HPP
00029 #define __SGL_UNSCALE_RANGE_HPP
00030 
00031 #include <sgl.h>
00032 #include <sglGroup.hpp>
00033 
00073 class SGL_DLL_API sglUnScaleRange : public sglGroup
00074 {
00075 public:
00077    sglUnScaleRange();
00078 
00080    virtual ~sglUnScaleRange();
00081 
00092    void setScaleParameters(float reference_size,
00093                            unsigned int min_pixel_size,
00094                            unsigned int max_pixel_size);
00095 
00100    unsigned int getMinPixelSize() const { return m_min_pixel_size; }
00101 
00106    unsigned int getMaxPixelSize() const { return m_max_pixel_size; }
00107 
00111    float getReferenceSize() const { return m_reference_size; }
00112 
00118    void setInverseMode(bool on) { m_inverse_mode = on; }
00119 
00123    bool getInverseMode() const { return m_inverse_mode; }
00124 
00125    // See sglNode::intersect(f) for details.
00126    virtual sglBound::IntersectResultEnum intersect(
00127       sglIntersectf &isector) const;
00128 
00129    // See sglNode::intersect(d) for details.
00130    virtual sglBound::IntersectResultEnum intersect(
00131       sglIntersectd &isector) const;
00132 
00133    // See sglNode::pick(f) for details.
00134    virtual void pick(sglPickf &pick_state, unsigned int cull_flags) const;
00135 
00136    // See sglNode::pick(d) for details.
00137    virtual void pick(sglPickd &pick_state, unsigned int cull_flags) const;
00138 
00139    // See sglNode::clone() for details.
00140    virtual sglNode *clone(unsigned int mode) const;
00141 
00142    // See sglObject::printInfo() for details.
00143    virtual void printInfo(ostream &ostrm, const char *indent_string) const;
00144 
00145 protected:
00146    // See sglNode::cull(f) for details
00147    virtual void cull(sglCull<float> &trav_state,
00148                      unsigned int cull_flags) const;
00149 
00150    // See sglNode::cull(d) for details
00151    virtual void cull(sglCull<double> &trav_state,
00152                      unsigned int cull_flags) const;
00153 
00154    void copyTo(sglUnScaleRange *dst, unsigned int mode) const;
00155 
00156 private:
00157    sglUnScaleRange(const sglUnScaleRange &);              // not implemented
00158    sglUnScaleRange &operator=(const sglUnScaleRange &);   // not implemented
00159 
00160    template <class T>
00161    T computeScale(sglProbeTemplate<T> &probe) const
00162         {
00163             T scale = (T)1;
00164 
00165             if (probe.getOrigViewFrustum().getOrthoFlag())
00166             {
00167                 // HACK precompute this in sglProbeTemplate.
00168                 scale =
00169                     probe.getOrigViewFrustum().getPlane(
00170                         sglFrustum<T>::eUP).getDistance() +
00171                     probe.getOrigViewFrustum().getPlane(
00172                         sglFrustum<T>::eDOWN).getDistance();
00173             }
00174             else
00175             {
00176                 scale = -(probe.getModelViewMatrix())[3][2] *
00177                     tan((probe.getOrigViewFrustum().getFOVY())*0.5) * 2.0;
00178             }
00179 
00180             // do I need to check for zero scale?
00181 
00182             // compute size of reference size in pixels, the apparent size
00183             unsigned int viewport_x, viewport_y;
00184             probe.getViewportSize(viewport_x, viewport_y);
00185             T apparent_size = viewport_y*m_reference_size/scale;
00186 
00187             if (apparent_size < m_min_pixel_size && m_min_pixel_size != 0)
00188                 scale = (T)m_min_pixel_size/apparent_size;
00189             else if (apparent_size > m_max_pixel_size && m_max_pixel_size != 0)
00190                 scale = (T)m_max_pixel_size/apparent_size;
00191             else
00192                 scale = (T)1;
00193 
00194             if (!m_inverse_mode)
00195                 return scale;
00196             else
00197                 return (T)1/scale;
00198         }
00199 
00200    template <class T>
00201    T computeScale(sglCull<T> &trav_state) const
00202         {
00203             T scale = 1.0f;
00204 
00205             if (trav_state.getOrigFrustum().getOrthoFlag())
00206             {
00207                 // HACK precompute this in sglProbeTemplate.
00208                 scale =
00209                     trav_state.getOrigFrustum().getPlane(
00210                         sglFrustum<T>::eUP).getDistance() +
00211                     trav_state.getOrigFrustum().getPlane(
00212                         sglFrustum<T>::eDOWN).getDistance();
00213             }
00214             else
00215             {
00216                 scale = -(trav_state.getModelViewMatrix())[3][2] *
00217                     tan((trav_state.getOrigFrustum().getFOVY())*0.5) * 2.0;
00218             }
00219 
00220             // do I need to check for zero scale?
00221 
00222             // compute size of reference size in pixels, the apparent size
00223             unsigned int viewport_x, viewport_y;
00224             trav_state.getViewportSize(viewport_x, viewport_y);
00225             T apparent_size = viewport_y*m_reference_size/scale;
00226 
00227             if (apparent_size < m_min_pixel_size && m_min_pixel_size != 0)
00228                 scale = (T)m_min_pixel_size/apparent_size;
00229             else if (apparent_size > m_max_pixel_size && m_max_pixel_size != 0)
00230                 scale = (T)m_max_pixel_size/apparent_size;
00231             else
00232                 scale = (T)1;
00233 
00234             if (!m_inverse_mode)
00235                 return scale;
00236             else
00237                 return (T)1/scale;
00238         }
00239 
00240 private:
00241    float        m_reference_size;
00242    unsigned int m_min_pixel_size;
00243    unsigned int m_max_pixel_size;
00244    bool         m_inverse_mode;
00245 };
00246 
00247 #endif

Generated at Mon Jul 1 18:00:06 2002 for SGL by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001