density-map.cpp

Go to the documentation of this file.
00001 /*
00002  * density-map.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * See density-map.h
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "density-map.h"                // always include our own header first!
00036 
00037 #include <math.h>
00038 
00039 #include "perf/perf.h"
00040 
00041 
00042 namespace glut {
00043 
00044 
00045 static const int s_maxRGBA = 255;
00046 
00047 
00048 ////////////////////////////////////////////////////////////////////////////////
00049 //
00050 //      static helper methods
00051 //
00052 ////////////////////////////////////////////////////////////////////////////////
00053 
00054 static void
00055 setUpImage
00056 (
00057 IN int width,
00058 IN int height,
00059 OUT media::image_t& img
00060 )
00061 {
00062         ASSERT(width > 1, "Bad width: %d", width);
00063         ASSERT(height > 1, "Bad height: %d", height);
00064 
00065         img.clear();
00066         img.width = width;
00067         img.height = height;
00068         img.bit_depth = 24;
00069         img.color_format = media::eColorFormat_RGBA;
00070         img.bytes_per_pixel = 4;
00071         img.setImageType("DENSITY");
00072         img.allocate();
00073 }
00074 
00075 
00076 
00077 ////////////////////////////////////////////////////////////////////////////////
00078 //
00079 //      public API
00080 //
00081 ////////////////////////////////////////////////////////////////////////////////
00082 
00083 void
00084 createSphericalDensityMap
00085 (
00086 IN int size,
00087 IN const img_color_t& color,
00088 OUT media::image_t& img
00089 )
00090 {
00091         perf::Timer timer("createSphericalDensityMap");
00092         ASSERT(size > 1, "bad size: %d", size);
00093 
00094         setUpImage(size, size, img);
00095 
00096         // set up for radius calculations
00097         float r = 0.5 * (size - 1);
00098         float r2 = r * r;
00099         float cx = r;
00100         float cy = r;
00101         float invr = 1.0 / r;
00102 
00103         // loop through image
00104         img_color_t * p = (img_color_t *) img.data;
00105         for (int y = 0; y < size; ++y) {
00106                 float dy = cy - y;
00107                 float dy2 = dy * dy;
00108                 for (int x = 0; x < size; ++x, ++p) {
00109                         float dx = cx - x;
00110                         float dx2 = dx * dx;
00111                         float d2 = r2 - dx2 - dy2;
00112                         float z = (d2 > 0) ? sqrt(d2) : 0.0;
00113                         float q = z * invr;     // 0 <= q <= 1
00114                         int a = color.alpha * q;
00115                         p->set(color.red, color.green, color.blue, a);
00116                 }
00117         }
00118 }
00119 
00120 
00121 
00122 void
00123 createCircularDensityMap
00124 (
00125 IN int size,
00126 IN const img_color_t& color,
00127 OUT media::image_t& img
00128 )
00129 {
00130         perf::Timer timer("createCircularDensityMap");
00131         ASSERT(size > 1, "bad size: %d", size);
00132 
00133         setUpImage(size, size, img);
00134 
00135         // set up for radius calculations
00136         float r = 0.5 * (size - 1);
00137         float r2 = r * r;
00138         float cx = r;
00139         float cy = r;
00140         float threshold = 0.2 * r2;     // 20% of distance
00141         float inv = 0.0;
00142         if (threshold > 0) {
00143                 inv = 1.0 / threshold;
00144         }
00145 
00146         // loop through image
00147         img_color_t * p = (img_color_t *) img.data;
00148         for (int y = 0; y < size; ++y) {
00149                 float dy = cy - y;
00150                 float dy2 = dy * dy;
00151                 for (int x = 0; x < size; ++x, ++p) {
00152                         float dx = cx - x;
00153                         float dx2 = dx * dx;
00154                         float d2 = r2 - dx2 - dy2;
00155                         int alpha = 0;
00156                         if (d2 > threshold)
00157                                 alpha = color.alpha;
00158                         else if (d2 > 0)
00159                                 alpha = (int) (d2 * inv * color.alpha);
00160                         p->set(color.red, color.green, color.blue, alpha);
00161                 }
00162         }
00163 }
00164 
00165 
00166 
00167 void
00168 createLineSegmentDensityMap
00169 (
00170 IN int width,
00171 IN const img_color_t& color,
00172 OUT media::image_t& img
00173 )
00174 {
00175         perf::Timer timer("createHorizontalDensityMap");
00176 
00177         int height = (2 * width) / 3;
00178         setUpImage(width, height, img);
00179 
00180         // set up for horizontal calculations
00181         float midH = 0.5 * (height - 1);
00182         float q = 0.6;
00183         float alpha = 130.0 / powf(midH * midH, q);
00184         float invalpha = 1.0 / alpha;
00185 
00186         // loop through image
00187         img_color_t * p = (img_color_t *) img.data;
00188         for (int y = 0; y < height; ++y) {
00189                 float dy = midH - y;
00190                 float dy2 = dy * dy;
00191                 for (int x = 0; x < width; ++x, ++p) {
00192                         float dx = 0;
00193                         if (x < midH) {
00194                                 dx = midH - x;
00195                         } else if (x > width - 1 - midH) {
00196                                 dx = x - (width - 1 - midH);
00197                         }
00198                         float dx2 = dx * dx;
00199                         float r2 = dx2 + dy2;
00200                         float r2q = 0.0;
00201                         if (r2 > 0.0) {
00202                                 r2q = powf(r2, q);
00203                         }
00204                         float invr = 1.0;
00205                         if (r2q > invalpha) {
00206                                 invr = 1.0 / (alpha * r2q);
00207                         }
00208                         int a = img_color_t::eMaxComponent * invr;
00209                         p->set(color.red, color.green, color.blue, a);
00210                 }
00211         }
00212 }
00213 
00214 
00215 
00216 };      // glut namespace
00217