$search
00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2011, Robert Bosch LLC. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Robert Bosch nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 *********************************************************************/ 00036 #include <iostream> 00037 #include <iomanip> 00038 #include <algorithm> 00039 #include <iostream> 00040 #include <sstream> 00041 00042 #include "vlr/displayGL.h" 00043 00044 namespace vlr { 00045 00046 template<class T> 00047 bool DisplayGL::internalPaint1d() { 00048 T* data = &static_cast<Image<T>*> (imgBuf)->data()[slice_offset_]; 00049 for(uint32_t c=0; c<std::min((uint32_t)6, imgBuf->channels()); c++) { 00050 glColor3f(colors_1d[3*c], colors_1d[3*c+1], colors_1d[3*c+2]); 00051 glBegin(GL_LINES); 00052 for (uint32_t i=1; i<imgBuf->width()*imgBuf->height(); i++) { 00053 // data is normalized to [0,1] already 00054 glVertex2f((float)i-1, (*data-minval_)/(maxval_-minval_)*GLWidget::height()); data++; 00055 glVertex2f((float)i, (*data-minval_)/(maxval_-minval_)*GLWidget::height());// data++; 00056 } 00057 glEnd(); 00058 } 00059 return true; 00060 } 00061 00062 template<> 00063 bool DisplayGL::internalPaint1d<float>() { 00064 float* data = &static_cast<Image<float>*> (imgBuf)->data()[slice_offset_]; 00065 for(uint32_t c=0; c<std::min((uint32_t)6, imgBuf->channels()); c++) { 00066 glColor3f(colors_1d[3*c], colors_1d[3*c+1], colors_1d[3*c+2]); 00067 glBegin(GL_LINES); 00068 for (uint32_t i=1; i<imgBuf->width()*imgBuf->height(); i++) { 00069 // data is normalized to [0,1] already 00070 // glVertex2f((float)i-1, (*data-minval_)/(maxval_-minval_)*GLWidget::height()*1); data++; 00071 // glVertex2f((float)i, (*data-minval_)/(maxval_-minval_)*GLWidget::height()*1);// data++; 00072 glVertex2f((float)i-1, (*data)*GLWidget::height()); data++; 00073 glVertex2f((float)i, (*data)*GLWidget::height());// data++; 00074 } 00075 glEnd(); 00076 } 00077 return true; 00078 } 00079 00080 template<> 00081 bool DisplayGL::internalPaint1d<double>() { 00082 double* data = &static_cast<Image<double>*> (imgBuf)->data()[slice_offset_]; 00083 for(uint32_t c=0; c<std::min((uint32_t)6, imgBuf->channels()); c++) { 00084 glColor3f(colors_1d[3*c], colors_1d[3*c+1], colors_1d[3*c+2]); 00085 glBegin(GL_LINES); 00086 for (uint32_t i=1; i<imgBuf->width()*imgBuf->height(); i++) { 00087 // data is normalized to [0,1] already 00088 glVertex2d((float)i-1, (*data)*GLWidget::height()); data++; 00089 glVertex2d((float)i, (*data)*GLWidget::height()); 00090 } 00091 glEnd(); 00092 } 00093 return true; 00094 } 00095 00096 void DisplayGL::internalPaint1d() { 00097 00098 if(imgBuf->width() == 1 && imgBuf->height() == 1) {return;} 00099 if(width() == 0 || height() == 0) {return;} 00100 00101 glMatrixMode(GL_PROJECTION); 00102 glLoadIdentity(); 00103 float scaled_width = width()*scale_; 00104 float scaled_height = height()*scale_; 00105 glOrtho(0., scaled_width, 0., scaled_height, -1, 1); 00106 glViewport(0, 0, scaled_width, scaled_height); 00107 glMatrixMode(GL_MODELVIEW); 00108 glLoadIdentity(); 00109 00110 glLineWidth(1); 00111 glEnable(GL_BLEND); 00112 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 00113 // glBlendFunc(GL_ONE, GL_ZERO); 00114 glEnable(GL_LINE_SMOOTH); 00115 glColor3f(0.8, 0.8, 0.2); 00116 00117 if (imgBuf) { 00118 switch(data_type_) { 00119 case TYPE_UCHAR: 00120 internalPaint1d<unsigned char> (); 00121 break; 00122 00123 case TYPE_CHAR: 00124 internalPaint1d<char> (); 00125 break; 00126 00127 case TYPE_USHORT: 00128 internalPaint1d<unsigned short> (); 00129 break; 00130 00131 case TYPE_SHORT: 00132 internalPaint1d<short> (); 00133 break; 00134 00135 case TYPE_UINT: 00136 internalPaint1d<unsigned int> (); 00137 break; 00138 00139 case TYPE_INT: 00140 internalPaint1d<int> (); 00141 break; 00142 00143 case TYPE_FLOAT: 00144 internalPaint1d<float> (); 00145 break; 00146 00147 case TYPE_DOUBLE: 00148 internalPaint1d<double> (); 00149 break; 00150 } 00151 00152 glDisable(GL_BLEND); 00153 glDisable(GL_LINE_SMOOTH); 00154 00155 double minx=0.0; 00156 double maxx=(double)imgBuf->width()*imgBuf->height(); 00157 drawGrid(minx, minval_, maxx, maxval_); 00158 drawTags1d(); 00159 } 00160 } 00161 00162 void DisplayGL::drawTags1d() { 00163 00164 std::pair<ImageBase::tagCIter, ImageBase::tagCIter> range; 00165 glDisable(GL_DEPTH_TEST); 00166 glEnable(GL_BLEND); 00167 glEnable(GL_POLYGON_SMOOTH); 00168 00169 // // draw lines 00170 // imgBuf->lines(range); 00171 // glBegin(GL_LINES); 00172 // for (ImageBase::tagCIter pit = range.first; pit != range.second; ++pit) { 00173 // TagLine* ln = dynamic_cast<TagLine*> ((*pit).second); 00174 // glColor4f(ln->r(), ln->g(), ln->b(), ln->a()); 00175 // glVertex2f(ln->x0(), height() - 1 - ln->y0()); 00176 // glVertex2f(ln->x1(), height() - 1 - ln->y1()); 00177 // } 00178 // glEnd(); 00179 // 00180 // for (ImageBase::tagCIter pit = range.first; pit != range.second; ++pit) { 00181 // TagLine* ln = dynamic_cast<TagLine*> ((*pit).second); 00182 // glColor4f(ln->r(), ln->g(), ln->b(), ln->a()); 00183 // fr.drawString2D(ln->label(), ln->x1() + crossSize, height() - 1 - ln->y1() + crossSize); 00184 // } 00185 00186 // draw points 00187 imgBuf->points(range); 00188 00189 glBegin(GL_LINES); 00190 for (ImageBase::tagCIter pit = range.first; pit != range.second; ++pit) { 00191 TagPoint* pt = static_cast<TagPoint*> ((*pit).second); 00192 glColor4f(pt->r(), pt->g(), pt->b(), pt->a()); 00193 glVertex2f(pt->x() - crossSize, height() - 1 - pt->y()); 00194 glVertex2f(pt->x() + crossSize, height() - 1 - pt->y()); 00195 glVertex2f(pt->x(), height() - 1 - pt->y() - crossSize); 00196 glVertex2f(pt->x(), height() - 1 - pt->y() + crossSize); 00197 } 00198 glEnd(); 00199 00200 for (ImageBase::tagCIter pit = range.first; pit != range.second; ++pit) { 00201 TagPoint* pt = static_cast<TagPoint*> ((*pit).second); 00202 glColor4f(pt->r(), pt->g(), pt->b(), pt->a()); 00203 fr.drawString2D(pt->label(), pt->x() + crossSize, height() - 1 - pt->y() + crossSize); 00204 } 00205 00206 glDisable(GL_POLYGON_SMOOTH); 00207 glDisable(GL_BLEND); 00208 glEnable(GL_DEPTH_TEST); 00209 } 00210 00211 void DisplayGL::drawGrid(double& minx, double& miny, double& maxx, double& maxy) { 00212 double grid_x, grid_y; 00213 double xstep, ystep; 00214 double numsteps_x = 5, numsteps_y=5; 00215 00216 if(minx == maxx || miny == maxy) {return;} 00217 00218 xstep = rint((maxx-minx)/numsteps_x)*(double)GLWidget::width()/(maxx-minx); 00219 // ystep = rint((maxy-miny)/numsteps_y)*(double)height()/(maxy-miny); 00220 ystep = (double)GLWidget::height()/(numsteps_y); 00221 00222 glLineWidth(0.5); 00223 glEnable(GL_BLEND); 00224 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 00225 00226 glEnable(GL_LINE_SMOOTH); 00227 glColor3f(0.4, 0.4, 0.4); 00228 glBegin(GL_LINES); 00229 00230 for (grid_x = 0; grid_x <= width(); grid_x+=xstep) { 00231 glVertex3d(grid_x, 0, GRID_Z); 00232 glVertex3d(grid_x, height(), GRID_Z); 00233 } 00234 00235 if(grid_x != width()) { 00236 glVertex3d(width(), 0, GRID_Z); 00237 glVertex3d(width(), height(), GRID_Z); 00238 } 00239 00240 for (grid_y = 0; grid_y <= height(); grid_y+=ystep) { 00241 glVertex3d(0, grid_y, GRID_Z); 00242 glVertex3d(width(), grid_y, GRID_Z); 00243 } 00244 00245 if(grid_y != height()) { 00246 glVertex3d(0, height(), GRID_Z); 00247 glVertex3d(width(), height(), GRID_Z); 00248 } 00249 00250 glEnd(); 00251 00252 std::stringstream label; 00253 00254 // for (unsigned int i = 0; i < keys->size(); i++) { 00255 // res->addPoint((*keys)[i].x, (*keys)[i].y, label.str()); 00256 00257 double x_offset=10; 00258 double y_offset=-16; 00259 for (grid_x = 0; grid_x <= GLWidget::width(); grid_x+=xstep) { 00260 label.str(""); 00261 label << std::fixed << std::setprecision(3) << grid_x*(maxx-minx)/GLWidget::width()+minx; 00262 fr.drawString2D(label.str(), float(grid_x+x_offset), float(GLWidget::height() + y_offset)); 00263 } 00264 00265 if(grid_x != GLWidget::width()) { 00266 label.str(""); 00267 label << std::fixed << std::setprecision(3) << grid_x*(maxx-minx)/GLWidget::width()+minx; 00268 fr.drawString2D(label.str(), float(grid_x-5*x_offset), float(GLWidget::height() + y_offset)); 00269 } 00270 00271 // x_offset=10; 00272 // y_offset=-16; 00273 for (grid_y = 0; grid_y <= GLWidget::height(); grid_y+=ystep) { 00274 label.str(""); 00275 label << std::fixed << std::setprecision(3) << grid_y*(maxy-miny)/GLWidget::height()+miny; 00276 fr.drawString2D(label.str(), x_offset, float(grid_y + y_offset)); 00277 } 00278 00279 if(grid_y != GLWidget::height()) { 00280 label.str(""); 00281 label << std::fixed << std::setprecision(3) << grid_y*(maxy-miny)/GLWidget::height()+miny; 00282 fr.drawString2D(label.str(), x_offset, float(grid_y + y_offset)); 00283 } 00284 00285 glDisable(GL_LINE_SMOOTH); 00286 glDisable(GL_BLEND); 00287 } 00288 00289 const float DisplayGL::colors_1d[18] = {1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1}; 00290 00291 } // namespace vlr