![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsPointManager.cpp 00003 00004 Inherited from class BasePointManager 00005 00006 SUKITTI PUNAK (07/21/2005) 00007 UPDATE (07/30/2005) 00008 ******************************************************************************/ 00009 #include "TAPsPointManager.hpp" 00010 // Using Inclusion Model (i.e. definitions are included in declarations) 00011 // (this name.cpp is included in name.hpp) 00012 // Each friend is defined directly inside its declaration. 00013 00014 //----------------------------------------------------------------------------- 00015 // DEBUG ENABLE 00016 #ifdef TAPs_ENABLE_DEBUG 00017 #define DEBUG_MESSAGE_TAPs_POINT_MANAGER 00018 #endif 00019 //----------------------------------------------------------------------------- 00020 BEGIN_NAMESPACE_TAPs__OpenGL 00021 //============================================================================= 00022 //----------------------------------------------------------------------------- 00023 // Static Data Members 00024 template <typename T> const int PointManager<T>::K_NUM_POINTS = 10; 00025 template <typename T> const T PointManager<T>::K_REGULAR_SIZE = 10; 00026 template <typename T> const T PointManager<T>::K_HIGHLIGHTED_SIZE = 10; 00027 template <typename T> const T PointManager<T>::K_SELECTED_SIZE = 10; 00028 template <typename T> const Vector3<T> PointManager<T>::K_REGULAR_COLOR( 1.0, 1.0, 1.0 ); 00029 template <typename T> const Vector3<T> PointManager<T>::K_HIGHLIGHTED_COLOR( 0.0, 1.0, 0.0 ); 00030 template <typename T> const Vector3<T> PointManager<T>::K_SELECTED_COLOR( 1.0, 0.0, 0.0 ); 00031 //----------------------------------------------------------------------------- 00032 // Default Constructor 00033 template <typename T> 00034 PointManager<T>::PointManager () 00035 : BasePointManager<T>(), 00036 m_uiPointDisplayList( 0 ) 00037 {} 00038 //----------------------------------------------------------------------------- 00039 // Destructor 00040 template <typename T> 00041 PointManager<T>::~PointManager () 00042 {} 00043 //----------------------------------------------------------------------------- 00044 // Setup 00045 // creates a random cloud of points with random colors within a canonical view. 00046 template <typename T> 00047 void PointManager<T>::Setup ( OpenGLBaseViewManager<T> *pViewManager ) 00048 { 00049 m_pViewManager = pViewManager; 00050 m_svPointList.reserve( K_NUM_POINTS ); 00051 //---------------------------------------------------------------- 00052 // Create a random set of points in a canonical volume 00053 srand( time(NULL) ); 00054 for ( int i = 0; i < K_NUM_POINTS; ++i ) { 00055 PointStruct point; 00056 point.m_vPosition[0] = ( static_cast<T>( rand() )/RAND_MAX - 0.5 ) * 2; 00057 point.m_vPosition[1] = ( static_cast<T>( rand() )/RAND_MAX - 0.5 ) * 2; 00058 point.m_vPosition[2] = ( static_cast<T>( rand() )/RAND_MAX - 0.5 ) * 2; 00059 point.m_uiState = REGULAR; 00060 m_svPointList.push_back( point ); 00061 } 00062 } 00063 //----------------------------------------------------------------------------- 00064 // Cleanup 00065 template <typename T> 00066 void PointManager<T>::Cleanup () 00067 { 00068 m_svPointList.clear(); 00069 } 00070 //----------------------------------------------------------------------------- 00071 // GetPointPosition 00072 // returns a const reference to a point position given a valid index. 00073 // Point positions are in the world coordinates. 00074 template <typename T> 00075 const Vector3<T> & PointManager<T>::GetPointPosition ( int i ) const 00076 { 00077 assert( 0 <= i && i < static_cast<int>( m_svPointList.size() ) ); 00078 return m_svPointList[i].m_vPosition; 00079 } 00080 //----------------------------------------------------------------------------- 00081 // GetPointPosition 00082 // returns a non-const reference to a point position given a valid index. 00083 // Point positions are in the world coordinates. 00084 template <typename T> 00085 Vector3<T> & PointManager<T>::GetPointPosition ( int i ) 00086 { 00087 assert( 0 <= i && i < static_cast<int>( m_svPointList.size() ) ); 00088 return m_svPointList[i].m_vPosition; 00089 } 00090 //----------------------------------------------------------------------------- 00091 // SetPointPosition 00092 // sets the position of a point given a valid index. 00093 // Point positions are in the world coordinates. 00094 template <typename T> 00095 void PointManager<T>::SetPointPosition ( int i, const Vector3<T> &position ) 00096 { 00097 assert( 0 <= i && i < static_cast<int>( m_svPointList.size() ) ); 00098 m_svPointList[i].m_vPosition = position; 00099 } 00100 //----------------------------------------------------------------------------- 00101 // SetPointHighlighted 00102 // controls the highlighted state of a point. 00103 template <typename T> 00104 void PointManager<T>::SetPointHighlighted ( int i, bool bHighlighted ) 00105 { 00106 assert( 0 <= i && i < static_cast<int>( m_svPointList.size() ) ); 00107 if ( bHighlighted ) { 00108 m_svPointList[i].m_uiState |= HIGHLIGHTED; 00109 } 00110 else 00111 { 00112 m_svPointList[i].m_uiState &= ~HIGHLIGHTED; 00113 } 00114 } 00115 //----------------------------------------------------------------------------- 00116 // IsPointHighlighted 00117 // queries if a point is highlighted. 00118 template <typename T> 00119 bool PointManager<T>::IsPointHighlighted ( int i ) const 00120 { 00121 assert( 0 <= i && i < static_cast<int>( m_svPointList.size() ) ); 00122 return ( m_svPointList[i].m_uiState & HIGHLIGHTED ) != 0; 00123 } 00124 //----------------------------------------------------------------------------- 00125 // SetPointSelected 00126 // controls the selected state of a point. 00127 template <typename T> 00128 void PointManager<T>::SetPointSelected ( int i, bool bSelected ) 00129 { 00130 assert( 0 <= i && i < static_cast<int>( m_svPointList.size() ) ); 00131 if ( bSelected ) { 00132 m_svPointList[i].m_uiState |= SELECTED; 00133 } 00134 else 00135 { 00136 m_svPointList[i].m_uiState &= ~SELECTED; 00137 } 00138 } 00139 //----------------------------------------------------------------------------- 00140 // IsPointSelected 00141 // queries if a point is selected. 00142 template <typename T> 00143 bool PointManager<T>::IsPointSelected ( int i ) const 00144 { 00145 assert( 0 <= i && i < static_cast<int>( m_svPointList.size() ) ); 00146 return ( m_svPointList[i].m_uiState & SELECTED ) != 0; 00147 } 00148 //----------------------------------------------------------------------------- 00149 // DrawPoints 00150 // Points can be drawn in different colors based on their highlighted and 00151 // selected states. Each point is drawn as a lit sphere. 00152 template <typename T> 00153 void PointManager<T>::DrawPoints () 00154 { 00155 glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT ); 00156 //---------------------------------------------------------------- 00157 if ( !m_uiPointDisplayList ) { 00158 m_uiPointDisplayList = glGenLists( 1 ); 00159 glNewList( m_uiPointDisplayList, GL_COMPILE ); 00160 GLUquadricObj *qObj = gluNewQuadric(); 00161 gluSphere( qObj, 0.5, 10, 10 ); 00162 gluDeleteQuadric( qObj ); 00163 glEndList(); 00164 } 00165 //---------------------------------------------------------------- 00166 glEnable( GL_COLOR_MATERIAL ); 00167 //---------------------------------------------------------------- 00168 // Draw each point as a lit sphere 00169 GLdouble dPointSize; 00170 for ( int i = 0; i < GetNumPoints(); ++i ) { 00171 if ( IsPointHighlighted(i) ) { 00172 dPointSize = K_HIGHLIGHTED_SIZE; 00173 glColor3dv( K_HIGHLIGHTED_COLOR ); 00174 } 00175 else if ( IsPointSelected(i) ) { 00176 dPointSize = K_SELECTED_SIZE; 00177 glColor3dv( K_SELECTED_COLOR ); 00178 } 00179 else { 00180 dPointSize = K_REGULAR_SIZE; 00181 glColor3dv( K_REGULAR_COLOR ); 00182 } 00183 00184 glPushMatrix(); 00185 const Vector3<T> &pos = GetPointPosition(i); 00186 glTranslated( pos[0], pos[1], pos[2] ); 00187 T windowToWorldScale = m_pViewManager->GetWindowToWorldScale(); 00188 T dPointScale = dPointSize * windowToWorldScale; 00189 glScaled( dPointScale, dPointScale, dPointScale ); 00190 glCallList( m_uiPointDisplayList ); 00191 glPopMatrix(); 00192 } 00193 //---------------------------------------------------------------- 00194 glPopAttrib(); 00195 } 00196 //----------------------------------------------------------------------------- 00197 // DrawLines 00198 // draws the line segments that connect the points. 00199 template <typename T> 00200 void PointManager<T>::DrawLines () 00201 { 00202 glPushAttrib( GL_ENABLE_BIT ); 00203 //---------------------------------------------------------------- 00204 glDisable( GL_LIGHTING ); 00205 glEnable( GL_COLOR_MATERIAL ); 00206 glColor3f( 1.0, 0.0, 0.0 ); 00207 glLineWidth( 2.0 ); 00208 glBegin( GL_LINE_STRIP ); 00209 for ( int i = 0; i < GetNumPoints(); ++i ) { 00210 glVertex3dv( GetPointPosition(i) ); 00211 } 00212 glEnd(); 00213 //glEnable( GL_LIGHTING ); 00214 //---------------------------------------------------------------- 00215 glPopAttrib(); 00216 } 00217 //----------------------------------------------------------------------------- 00218 // PickPoint 00219 // finds the closest point to the x, y screen coordinates on the near plane 00220 template <typename T> 00221 int PointManager<T>::PickPoint( int x, int y ) const 00222 { 00223 T minDistSqr = DBL_MAX; 00224 int iClosestPoint = -1; 00225 //---------------------------------------------------------------- 00226 for ( int i = 0; i < GetNumPoints(); ++i ) { 00227 Vector3<T> win; 00228 Vector3<T> obj = GetPointPosition(i); 00229 assert( m_pViewManager->ToScreen( obj, win ) ); 00230 //------------------------------------------------------ 00231 T distX = win[0] - x; 00232 T distY = win[0] - y; 00233 T distSqr = ( distX*distX + distY*distY ); 00234 if ( distSqr < minDistSqr ) { 00235 minDistSqr = distSqr; 00236 iClosestPoint = i; 00237 } 00238 } 00239 return iClosestPoint; 00240 } 00241 //----------------------------------------------------------------------------- 00242 //============================================================================= 00243 END_NAMESPACE_TAPs__OpenGL 00244 //----------------------------------------------------------------------------- 00245 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00246 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8