Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "density-map.h"
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
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
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
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
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;
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
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;
00141 float inv = 0.0;
00142 if (threshold > 0) {
00143 inv = 1.0 / threshold;
00144 }
00145
00146
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
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
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 };
00217